package com.ea.admin.controller.superAdmin;

import com.ea.admin.permission.PermissionCode;
import com.ea.admin.pojo.Enum.Permission;
import com.ea.admin.pojo.param.datasource.*;
import com.ea.admin.pojo.vo.datasource.*;
import com.ea.admin.service.DataSourceService;
import com.ea.admin.utils.NowUserGetter;
import com.ea.common.log.LogAnnotation;
import com.ea.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@Api(tags = "超级管理-数据源管理")
@RequestMapping("/admin/super/datasource")
public class SuperDataSourceController {
    
    @Resource
    private DataSourceService dataSourceService;
    
    @Resource
    private NowUserGetter nowUserGetter;


    @Operation(summary = "通过数据源名和/或数据源ip获取数据源列表")
    @GetMapping("/getDataSourceList")
    @ApiImplicitParam(dataType = "GetDataSourceListParam", name = "getDataSourceListParam", value = "获取数据源列表", readOnly = true, required = true)
    @LogAnnotation(module = "SuperDataSourceController", operation = "获取数据源列表")
    @Permission(permission_code = PermissionCode.DATA_SOURCE_MANAGE)
    public Result<GetDataSourceListVo> getDataSourceList(@RequestBody @Valid GetDataSourceListParam param) {
        GetDataSourceListVo result = dataSourceService.getDataSourceList(
                nowUserGetter.getNowUser().getAccountId(),
                param.getSourceName(),
                param.getSourceIp(),
                param.getPage(),
                param.getPageSize()
        );
        return Result.success(result);
    }
    
    @Transactional
    @Operation(summary = "添加数据源")
    @PostMapping("/addDataSource")
    @ApiImplicitParam(dataType = "AddDataSourceParam", name = "addDataSourceParam", value = "添加数据源", readOnly = true, required = true)
    @LogAnnotation(module = "SuperDataSourceController", operation = "添加数据源")
    @Permission(permission_code = PermissionCode.ADD_DATA_SOURCE,
            content = "添加数据源")
    public Result<DataSourceVo> addDataSource(@RequestBody @Valid AddDataSourceParam param) {
        DataSourceVo result = dataSourceService.addDataSource(
                nowUserGetter.getNowUser().getAccountId(),
                param
        );
        return Result.success(result);
    }
    
    @Transactional
    @Operation(summary = "编辑数据源")
    @PutMapping("/editDataSource")
    @ApiImplicitParam(dataType = "EditDataSourceParam", name = "editDataSourceParam", value = "编辑数据源", readOnly = true, required = true)
    @LogAnnotation(module = "SuperDataSourceController", operation = "编辑数据源")
    @Permission(permission_code = PermissionCode.EDIT_DATA_SOURCE,
            content = "编辑数据源")
    public Result editDataSource(@RequestBody @Valid EditDataSourceParam param) {
        DataSourceVo result = dataSourceService.editDataSource(
                nowUserGetter.getNowUser().getAccountId(),
                param
        );
        return Result.success(result);
    }
    
    @Transactional
    @Operation(summary = "删除数据源")
    @DeleteMapping("/deleteDataSource")
    @ApiImplicitParam(dataType = "DeleteDataSourceParam", name = "deleteDataSourceParam", value = "删除数据源", readOnly = true, required = true)
    @LogAnnotation(module = "SuperDataSourceController", operation = "删除数据源")
    @Permission(permission_code = PermissionCode.DELETE_DATA_SOURCE,
            content = "删除数据源")
    public Result deleteDataSource(@RequestBody @Valid DeleteDataSourceParam param) {
       String dataSourceName = dataSourceService.deleteDataSource(
                nowUserGetter.getNowUser().getAccountId(),
                param
        );
        return Result.success(dataSourceName);
    }


}